﻿using System;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
using TpAgentCore.Base;
using TpBaseObjects.Base;
using TpLogger;
using TpTools;
using TpIntern.GUI;
using System.Reflection;
using TpData.Manager;
using TpContracts;

namespace TpIntern {
    class Core {

        #region Members

        public const string tpContextMenuName = "Ply";
        private const string errType = "Err";
        private const string logType = "Log";
        private const string logArea = "TpIntern.Core";

        private enum faceIDs {
            NewSheet = 343,
            Export = 352,
            Import = 342,
            Dokument = 341,
            Bearbeitung = 345
        }

        #endregion

        #region Kontextmenü

        #region Export + Funktion

        public static void ExportLeistungenIntern(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            try {
                Agent agent = new Agent();
                agent.UpdateLeistungenIntern(wb, sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ExportLeistungenIntern: " + ex.Message, true);
            }
        }

        #endregion

        #region Ansicht

        public static void ChangeView(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            Agent agent = new Agent();
            wb.Save();
            //Auswahl awAuswahl = new Auswahl();
            SelectForm awAuswahl = new SelectForm();
            awAuswahl.ShowDialog();

            if (awAuswahl.Cancel) {
                return;
            }

            agent.ChangeView(sh, awAuswahl.DateFrom, awAuswahl.DateTo, awAuswahl.Content, awAuswahl.Sorting, awAuswahl.SortOrder, awAuswahl.ShowType);
            ((Microsoft.Office.Interop.Excel._Workbook)wb).Activate();
        }

        public static void ChangeViewWeek(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "K, P";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByWeek(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewWeek: " + ex.Message, true);
            }
        }

        public static void ChangeViewD2(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "O,K";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByD2(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewD2: " + ex.Message, true);
            }
        }

        public static void ChangeViewLeistung(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "AH";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByLeistung(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewLeistung: " + ex.Message, true);
            }
        }

        public static void ChangeViewOrganisation(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "W,X";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByOrganisation(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewOrganisation: " + ex.Message, true);
            }
        }

        public static void ChangeViewTorprofi(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "C,D,E,F,G,AH";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByTorprofi(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewTorprofi: " + ex.Message, true);
            }
        }

        public static void ChangeViewUser(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "B,C,D,E,F,G,AH";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByUser(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewUser: " + ex.Message, true);
            }
        }

        public static void ChangeViewVr(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            int endRow = 0;
            string search = "H,C,D,E,F,G,AH";
            string[] columns = search.Split(',');
            try {
                Agent agent = new Agent();
                agent.ClearJobSheet(sh, exApp, out endRow);
                agent.SheetSort(sh, 3, endRow - 1, columns, TpExtensions.Extensions.tpSortOrder.Up);
                agent.SheetGroupByVr(sh);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ChangeViewVr: " + ex.Message, true);
            }
        }

        public static void ExportJobevidenzIntern(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            try {
                Agent agent = new Agent();
                agent.UpdateJobEvidenzIntern(new string[] { wb.FullName }, false);
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "ExportJobevidenzIntern: " + ex.Message, true);
            }
        }



        #endregion

        #endregion

        #region Workbook Events

        public static void WBActivate(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            SetKontextMenu(exApp, wb, sh, false);
        }

        public static void WBDeactivate(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            DeleteMenuEntries(exApp);
        }

        public static void WBSheetFollowHyperlink(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Range rg) {
            LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "WBSheetFollowHyperlink: (" + wb.Name + "), (" + sh.Name + ")", false);
            FollowAndDeleteHyperlink(exApp, wb, sh, rg);
        }

        #endregion

        #region Methods

        public static void AddMenuEntries() {
            try {
                if (GlobalVar.MenuEntriesIntern.Count == 0) {

                    //View
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, GroupHeader = true, MenuName = "ChangeViewGroup", Caption = "Ansicht", FaceID = (int)faceIDs.NewSheet, Enabled = true, NewGroup = true, Visibly = true, EverySheet = true }, true);
                    //GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeView", Caption = "Ansicht ändern", OnAction = "VBSub_1", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewWeek", Caption = "chronologisch", OnAction = "VBSub_2", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewD2", Caption = "D2", OnAction = "VBSub_7", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewLeistung", Caption = "Leistung", OnAction = "VBSub_8", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewOrganisation", Caption = "Org, Per", OnAction = "VBSub_9", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewTorprofi", Caption = "Torprofi", OnAction = "VBSub_5", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewUser", Caption = "User", OnAction = "VBSub_4", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    GlobalVar.AddMenu(new MenuEintrag { GroupID = 1, MenuName = "ChangeViewVr", Caption = "Vr", OnAction = "VBSub_6", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);


                    //Export (int)faceIDs.Export
                    //GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, GroupHeader = true, MenuName = "GruppeExportIntern", Caption = "Export + Funktion Intern", FaceID = (int)faceIDs.Export, Enabled = true, NewGroup = true, Visibly = true, EverySheet = true }, true);
                    //GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, MenuName = "ExportLeistungenIntern", Caption = "Leistungen Intern", OnAction = "VBSub_18", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true, }, true);
                    //GlobalVar.AddMenu(new MenuEintrag { GroupID = 2, MenuName = "EinfügenLeistungenIntern", Caption = "Leistungen Intern", OnAction = "VBSub_18", FaceID = 0, Enabled = true, NewGroup = false, Visibly = true }, true);
                }
            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "AddMenuEntriesIntern: " + ex.Message, true);
            }
        }

        private static void DeleteMenuEntries(Microsoft.Office.Interop.Excel.Application exApp) {
            DeleteMenuEntries(exApp, null);
        }

        private static void DeleteMenuEntries(Microsoft.Office.Interop.Excel.Application exApp, string menuName) {
            if (menuName == null) {
                foreach (MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                    if (KontextMenuControlExists(exApp, me.Caption)) {
                        exApp.CommandBars[tpContextMenuName].Controls[me.Caption].Delete();
                    }
                }
            } else {
                foreach (MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                    if (me.Caption == menuName) {
                        if (KontextMenuControlExists(exApp, me.Caption)) {
                            exApp.CommandBars[tpContextMenuName].Controls[me.Caption].Delete();
                        }
                    }
                }
            }
        }

        private static void FollowAndDeleteHyperlink(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh) {
            FollowAndDeleteHyperlink(exApp, wb, sh, null);
        }

        private static void FollowAndDeleteHyperlink(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, Range myRange) {
            Range r1;

            if (myRange != null) {
                r1 = myRange;
            } else {
                r1 = exApp.ActiveCell;
            }

            LogDataManager.SaveLog(Contracts.MyUser.UserID, logType, logArea, "FollowAndDeleteHyperlink: (" + r1.Address + " (" + r1.Hyperlinks.Count.ToString() + ")" , false);
            if (r1.Hyperlinks.Count > 0) {
                int row = r1.Row;
                try {
                    Range r2 = sh.Rows[row, Missing.Value];
                    r2.Delete(Shift: XlDeleteShiftDirection.xlShiftUp);
                } catch (Exception ex) {
                    LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "FollowAndDeleteHyperlink: " + ex.Message, true);
                }
            }
        }

        public static void KontextMenuAddControl(Microsoft.Office.Interop.Excel.Application exApp) {

            foreach (MenuEintrag me in GlobalVar.MenuEntriesIntern) {
                try {
                    if (!KontextMenuControlExists(exApp, me.Caption)) {
                        CommandBarButton cbb = (CommandBarButton)exApp.CommandBars[tpContextMenuName].Controls.Add();
                        cbb.Caption = me.Caption;
                        cbb.OnAction = me.OnAction;
                        cbb.FaceId = me.FaceID;
                        cbb.BeginGroup = me.NewGroup;
                        cbb.Enabled = me.Enabled;
                        cbb.Height = 0;
                    }
                } catch (Exception ex) {
                    LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "KontextMenuAddControl: (" + me.Caption + ") " + ex.Message, true);
                }
            }
        }

        public static void SetKontextMenu(Microsoft.Office.Interop.Excel.Application exApp, Workbook wb, Worksheet sh, bool delete) {
            try {
                if (!wb.FullName.Contains("je.xlsm")) {
                    DeleteMenuEntries(exApp);
                    return;
                }

                AddMenuEntries();

                KontextMenuAddControl(exApp);


            } catch (Exception ex) {
                LogDataManager.SaveLog(Contracts.MyUser.UserID, errType, logArea, "SetKontextMenu: " + ex.Message, true);
            }
        }

        #endregion

        #region Functions

        private static bool KontextMenuControlExists(Microsoft.Office.Interop.Excel.Application exApp, string caption) {
            foreach (CommandBarButton cbb in exApp.CommandBars[tpContextMenuName].Controls) {
                if (cbb.Caption == caption) {
                    return true;
                }
            }
            return false;
        }

        #endregion

    }
}
